package wiz.client;

import com.google.gwt.core.client.GWT;

import wiz.client.model.WizGameModel;
import wiz.client.model.WizGameRound;
import wiz.client.model.WizPlayerRoundScore;

public class PSSWizGameDetails implements GameDetails {

	private final WizStrings fWizStrings = GWT.create(WizStrings.class);
	
	@Override
	public int calculateNewScore(int currentScore, WizPlayerRoundScore roundResult) {
		if(roundResult.getTricksBid() ==roundResult.getTricksMade()){
			currentScore += 20;
		}
		
		int diff = roundResult.getTricksBid() - roundResult.getTricksMade();
		if(diff > 0){
			diff *= -1;
		} else if(diff == 0){
			diff = roundResult.getTricksBid();
		}
		
		currentScore += diff * 10;
		return currentScore;
	}

	@Override
	public int suggestNewHandSize(WizGameModel model) {
		return 0;
	}

	@Override
	public boolean isValidHandSize(int handSize) {
		return handSize > 0;
	}

	@Override
	public String isValidBid(WizGameRound round) {
		int handSize = round.getRoundHandSize();
		int bidSum = 0;
		for (WizPlayerRoundScore playerRoundScore : round.getRoundScores()) {
			if(playerRoundScore.getTricksBid() < 0){
				return  fWizStrings.playersBidLessThanZero(playerRoundScore.getPlayer().getName());
			}
			
			if(playerRoundScore.getTricksBid() > handSize){
				return  fWizStrings.playerBidLargerThanHandSize(playerRoundScore.getPlayer().getName());
			}
			
			bidSum += playerRoundScore.getTricksBid();
		}
		
		
		if(bidSum == handSize){
			return fWizStrings.totalBidsCannotEqualHandSize();
		} else {
			return null;
		}
		
	}

	@Override
	public String isValidTricksMade(WizGameRound round) {
		int handSize = round.getRoundHandSize();

		int bidSum = 0;
		for (WizPlayerRoundScore playerRoundScore : round.getRoundScores()) {
			if(playerRoundScore.getTricksMade() < 0){
				return fWizStrings.tricksMadeMustBePositive(playerRoundScore.getPlayer().getName());
			}
			bidSum += playerRoundScore.getTricksMade();
		}
		
		
		if(bidSum != handSize){
			return  fWizStrings.tricksMadeMustTotalRoundSize();
		} else {
			return null;
		}
	}


}
